﻿using System;
using System.Web;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Common;
using log4net;
using SqlSugar;
using AutoFacUtils;
using CashFlow.ShopManage.Def;
using CashFlow.Db.DAL;
using CashFlow.Db.Entity;
using CashFlow.Db.Repository;
using CashFlow.ShopManage.Service;

namespace CashFlow.ShopManage.Service.Implement
{
    public class AppointmentService : Service<AppointmentEntity>, IAppointmentService
    {
        private IAdminService GetAdminService() { return AutoFacHelper.Get<IAdminService>(); }
        private IUserService GetUserService() { return AutoFacHelper.Get<IUserService>(); }

        public IList<AppointmentUserInfoView2> QueryByWhere(int shopManageUserId
            , string date
            , string name
            , string type)
        {
            var userService = GetUserService();
            var userIds = userService.QueryShopAllUsersIdByShopManagerUserId(
                            shopManageUserId
                            , null);
            using (var db = SqlSugarHelper.GetDb())
            {
                var query = db.Queryable<AppointmentEntity>()
                                .InnerJoin<UserInfoEntity>((a, u) => a.UserId == u.Id)
                                .Where((a, u) => userIds.Contains(a.UserId)
                                        && a.Time.ToString("yyyy-MM-dd") == date)
                                .OrderByDescending((a, u) => a.Time);

                if (!string.IsNullOrEmpty(name))
                {
                    query = query.Where((a, u) => u.Name.Contains(name));
                }
                if (!string.IsNullOrEmpty(type))
                {
                    query = query.Where((a, u) => a.Type == type);
                }

                var ls = query.Select((a, u) => new AppointmentUserInfoView2
                {
                    AppointmentInfo = a,
                    UserInfo = u
                }).ToList();

                return ls;
            }
        }
    }
}